import json
import random

from foolishbird import HackChatBot


class FoolishBird(HackChatBot):
	def __init__(self):
		super().__init__(
			name="foolishbird",
			password="foolishbird",
			channel="your-channel")
		self.info_count = 0

	def on_message(self,ws,message):
		data = json.loads(message)		
		data_type = data["cmd"]

		if data_type == "onlineSet":
			self.show_mes(self.info(f'Online Users:{str(",".join(data["nicks"]))}'))

		if data_type == "chat":
			if "color" in data and "trip" not in data:
				name = f'<h4><font color=#{data["color"]}>{data["nick"]}</font></h4>'
			elif "trip" in data and "color" not in data:
				name = f'<h4>{data["trip"]} {data["nick"]}</h4>'
			elif "trip" in data and "color" in data:
				name = f'<h4>{data["trip"]} <font color=#{data["color"]}>{data["nick"]}</font></h4>'
			else:
				name = f'<h4>{data["nick"]}</h4>'
			self.show_mes(f'{name} {data["text"]} <br>')

		if data_type == "info":
			self.show_mes(self.info(f'{data["text"]} <br>'))

		if data_type == "onlineRemove":
			self.show_mes(self.info(f'{data["nick"]} left <br>'))

		if data_type == "onlineAdd":
			self.show_mes(self.info(f'{data["nick"]} joined <br>'))

		if data_type == "emote":
			self.show_mes(self.info(f'{data["text"]} <br>'))

	def info(self,message,color="#000000"):
		self.info_count += 1
		if self.info_count == 2:
			return f"<h3>* <font color={color}>{message}</font></h3>"
		else:
			return f"<h3>* <font color={color}>{message}</font></h3>"

	def show_mes(self,message):
		self.gui.widgets["message_text"].append(message)
		self.gui.widgets["message_text"].moveCursor(self.gui.widgets["message_text"].textCursor().End)

if __name__ == '__main__':
	foolishbird = FoolishBird()
	foolishbird.setup(gui=True)

